package cn.group.controller;

import cn.group.service.InformationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import pojo.entity.ImgDo;
import pojo.entity.Information;
import pojo.vo.BaseRespVO;
import utils.BaseController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Api(tags = "资讯信息")
@Configuration
@RequestMapping(value = "/information")
public class InformationController extends BaseController {
    @Autowired
    private InformationService informationService;

    @ApiOperation("添加资讯")
    @PostMapping(value = "/addInformation.do")
    @ResponseBody
    public BaseRespVO addInformation(@RequestBody Information information, HttpServletResponse response, HttpServletRequest request) {
        int insert = informationService.insert(information);
        BaseRespVO result = new BaseRespVO<>();
        result.setData(insert);
        return result;
    }

    @ApiOperation("文件上传")
    @PostMapping(value = "/upload")
    @ResponseBody
    public String upload(@ApiParam(value = "文件") MultipartFile file, HttpServletResponse response) {
        System.out.println("文件存入本地资源");
        if (file.isEmpty()) {
            throw new RuntimeException("文件上传失败");
        }


        String string="imgs/";

//获得文件地址
        String baseDir = "D:\\log\\imgs";
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
//获得时间
        String dateStr = dateFormat.format(new Date());
//目录地址
        String dir = baseDir + File.separator + dateStr;
        string=string+dateStr+"/";
//判断目录是否存在
        File dirFile = new File(dir);
        if (!dirFile.exists()){
            dirFile.mkdirs();
            ImgDo imgDo = new ImgDo();
            String imgdoPath ="imgs/";
            imgdoPath = imgdoPath+dateStr;
            imgDo.setImgPath(imgdoPath);
            imgDo.setImgName("资讯");
            String imgdoTags = imgdoPath+" 资讯";
            imgDo.setImgTags(imgdoTags);
            System.out.println(imgDo);
            informationService.saveDateToImgPath(imgDo);
            dirFile = new File(dir);
        }

//在当前文件下存文件并保证文件名不重复
//获取文件名的后缀，分割 file.getOriginalFilename()-->原文件名
        String oldFileName = file.getOriginalFilename();
//分割,拿到文件后缀
        String fileSuffix = oldFileName.substring(oldFileName.lastIndexOf("."), oldFileName.length());
//获取时间戳，进行拼接-->得到文件名
        String fileName = System.currentTimeMillis() + fileSuffix;
//文件保存的地址 = 目录地址 + 分割符 + 文件名
        String path = dir + "/" + fileName;

//文件保存地址
        string=string+fileName;
//保存文件
        try {
            file.transferTo(new File(path));
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("string:"+string);
        return writerSuccessResult(string, response);
    }

    @ApiOperation("根据资讯ID删除资讯")
    @RequestMapping(value = "/deleteInformation.do", method = RequestMethod.POST)
    @ResponseBody
    public String deleteInformation(@RequestBody Long informationId, HttpServletResponse response) {
        return writerSuccessResult(informationService.deleteById(informationId), response);
    }

    @ApiOperation("根据资讯ID获取资讯信息")
    @RequestMapping(value = "/selectByIdInformation.do", method = RequestMethod.POST)
    @ResponseBody
    public String selectByIdInformation(@RequestBody Long informationId, HttpServletResponse response) {
        return writerSuccessResult(informationService.selectById(informationId), response);
    }

    @ApiOperation("根据资讯分类获取资讯列表")
    @RequestMapping(value = "/selectByCategoryIdInformation.do", method = RequestMethod.POST)
    @ResponseBody
    public String selectByCategoryIdInformation(@RequestBody Long categoryId, HttpServletResponse response) {
        return writerSuccessResult(informationService.selectByCategoryId(categoryId), response);
    }

    @ApiOperation("获取所有资讯")
    @RequestMapping(value = "/selectListInformation.do", method = RequestMethod.GET)
    @ResponseBody
    public String selectListInformation(HttpServletResponse response) {
        return writerSuccessResult(informationService.selectList(), response);
    }

    @ApiOperation("获取前10资讯")
    @RequestMapping(value = "/selectTenListInformation.do", method = RequestMethod.GET)
    @ResponseBody
    public String selectTenListInformation(HttpServletResponse response) {
        List list = informationService.selectTenList();
        System.out.println("selectTenListInformation: " + list);
        return writerSuccessResult(informationService.selectTenList(), response);
    }

    @ApiOperation("获取前11资讯")
    @RequestMapping(value = "/viewListInformation.do", method = RequestMethod.GET)
    @ResponseBody
    public String viewListInformation(HttpServletResponse response) {
        return writerSuccessResult(informationService.viewList(), response);
    }

    @ApiOperation("根据发布状态改变指定状态")
    @RequestMapping(value = "/updateStatusByIdInformation.do", method = RequestMethod.POST)
    @ResponseBody
    public String updateStatusByIdInformation(@RequestBody Information information, HttpServletResponse response) {
        return writerSuccessResult(informationService.updateStatusById(information), response);
    }

    @ApiOperation("模糊搜索")
    @RequestMapping(value = "/dimSelectListAllInformation.do", method = RequestMethod.POST)
    @ResponseBody
    public String dimSelectListAllInformation(@RequestBody Information information, HttpServletResponse response) {
        return writerSuccessResult(informationService.dimSelectList(information), response);
    }

    @ApiOperation("模糊搜索")
    @RequestMapping(value = "/dimSelectListInformation.do", method = RequestMethod.POST)
    @ResponseBody
    public String dimSelectListInformation(@RequestBody Information information, HttpServletResponse response) {
        information.setNewPages((information.getNewPages()-1)*information.getPages());
        System.out.println("informationService.dimSelectList(information):"+informationService.dimSelectList(information));
        return writerSuccessResult(informationService.dimSelectList(information), response);
    }

    @ApiOperation("修改")
    @RequestMapping(value = "/updateByInformationIdInformation.do", method = RequestMethod.POST)
    @ResponseBody
    public String updateByInformationIdInformation(@RequestBody Information information, HttpServletResponse response) {
        return writerSuccessResult(informationService.updateByInformationId(information), response);
    }

    @ApiOperation("修改浏览次数")
    @RequestMapping(value = "/viewCountInformation.do", method = RequestMethod.POST)
    @ResponseBody
    public String viewCountInformation(@RequestBody Information information, HttpServletResponse response) {
        return writerSuccessResult(informationService.viewCount(information), response);
    }

    @ApiOperation("分页")
    @RequestMapping(value = "/pagingInformation.do", method = RequestMethod.POST)
    @ResponseBody
    public String pagingInformation(@RequestBody Information information, HttpServletResponse response) {
        information.setNewPages((information.getNewPages()-1)*information.getPages());
        return writerSuccessResult(informationService.paging(information), response);
    }
}
